---
title: What are flows?
sidebarTitle: Introduction
---

<Tip>
Flows provide a shared context for all tasks and agents within a workflow.
</Tip>

Flows are the high-level containers that encapsulate AI-powered workflows. They allow you to compose ControlFlow tasks and traditional software into complex behaviors, making it easier to build sophisticated, controllable AI applications.

Flows act as the connective tissue in your AI application, providing a shared context for all tasks and agents within the workflow. This shared environment ensures that context, messages, and activity are maintained across multiple tasks.

## Why Flows Matter

Flows are essential to ControlFlow's approach to AI workflows for three key reasons:

1. **Integration of AI with Traditional Programming**: Flows allow you to combine AI tasks with standard Python control structures and logic. This means you can use if statements, loops, and other familiar programming constructs to control the flow of your AI application. You can easily implement conditional logic based on AI outputs or user inputs, creating dynamic and responsive AI workflows.

2. **Shared Context and Continuity**: Flows maintain a consistent state and context across all components of your AI workflow. This shared context goes beyond simply piping the result of one task into another. It allows for more natural, context-aware interactions across multiple tasks. For instance, an agent in a later task can reference information or decisions made in earlier tasks, even if that information wasn't explicitly passed as a parameter.

3. **Seamless Mixing of Eager and Lazy Execution**: Flows in ControlFlow offer the flexibility to mix eager task execution (using `cf.run()`) with more complex, dependency-based task structures. This allows you to create workflows that are both immediate and responsive, yet capable of handling complex, interdependent AI processes when needed.

## Flows in Practice

Here's an example that demonstrates the power of flows in creating a dynamic, interactive AI application with shared context:

```python
import controlflow as cf

@cf.flow
def user_onboarding_flow():
    name = cf.run("Get the user's name", interactive=True)
    age = cf.run("Get the user's age", result_type=int, interactive=True)

    if age < 13:
        cf.run("Explain our policy for users under 13. Your response should be in a form that can be directly displayed to the user.")
    else:
        interests = cf.run("Welcome the user and ask about their interests", context=dict(name=name), interactive=True, result_type=list[str])
        cf.run("Provide personalized recommendations", context=dict(name=name, interests=interests), interactive=True)

user_onboarding_flow()
```

This example showcases how flows enable:

1. Seamless integration of AI tasks with Python control flow (the `if` statement based on user age).
2. Maintenance of shared context throughout the entire conversation. Later tasks can reference earlier information about the user's name, even though it wasn't explicitly passed as a parameter.
3. Natural conversation flow that can include branching logic while maintaining coherence.

By using flows, you can create AI applications that are not just powerful, but also structured, maintainable, and capable of complex, context-aware interactions. This approach allows you to harness the full potential of AI while retaining the control and predictability expected in production software systems.